package cn.slf.ph.dao.impl;

import cn.slf.ph.dao.CourierStationDao;
import cn.slf.ph.entity.Community;
import cn.slf.ph.entity.CourierStation;
import cn.slf.ph.util.JDBCDruidUtil;

import javax.naming.Name;
import javax.sound.midi.Soundbank;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CourierStationDaoImpl implements CourierStationDao {
    //   获取小区名字
    @Override
    public String getCommunityName(CourierStation courierStation) {
        String sql=" select communityName from community where communityId = ? ";
        Object obj[]={courierStation.getCourierStationCommunityId()};
        ResultSet query = JDBCDruidUtil.query(sql, obj);
        String name=null;
        while (true) {
            try {
                if (!query.next()) break;
                name=query.getString(1);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        JDBCDruidUtil.close();
        return name;
    }
    @Override
    public int insert(CourierStation courierStation) {
        String CourierStationCommunityName = getCommunityName(courierStation);
        String sql = " insert into courierstation values (null,?,?,?,?,?,?,?,?,?,?,?,?) ";
        Object obj[] = {
                courierStation.getCourierStationName(),
                courierStation.getCourierStationImage(),
                CourierStationCommunityName,
                courierStation.getCourierStationOrderCount(),
                //courierStation.getAddTime(),
                //可以用系统当前时间来代替创建时间
                new Date(),
                courierStation.getCourierStationStatus(),
                courierStation.getCourierStationSort(),
                courierStation.getCourierStationProvince(),
                courierStation.getCourierStationDistrict(),
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationCity(),
                courierStation.getCourierStationCommunityId()
        };

//        JDBCDruidUtil.close();
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public int update(CourierStation courierStation) {
        String sql = " update courierstation set courierStationName=?,courierStationImage=?,courierStationCommunityName=?," +
                "courierStationOrderCount=?,courierStationStatus=?,courierStationSort=?," +
                "courierStationProvince=?,courierStationDistrict=?,courierStationAddress=?," +
                "courierStationCity=?,courierStationCommunityId=?  where courierStationId=? ";
        Object obj[] = {
                courierStation.getCourierStationName(),
                courierStation.getCourierStationImage(),
                courierStation.getCourierStationCommunityName(),
                courierStation.getCourierStationOrderCount(),
                //courierStation.getAddTime(),
                //可以用系统当前时间来代替创建时间
                //new Date(),  不用改 因为是创建的时间
                courierStation.getCourierStationStatus(),
                courierStation.getCourierStationSort(),
                courierStation.getCourierStationProvince(),
                courierStation.getCourierStationDistrict(),
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationCity(),
                courierStation.getCourierStationCommunityId(),
                courierStation.getCourierStationId()
        };
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public List<CourierStation> selectAll(Map<String, String> map) {
        List<CourierStation> list = new ArrayList<>();
        //形成动态的sql语句
        String sql = "select * from courierstation where 1=1 ";
        List<Object> params = new ArrayList<>();//不是固定的 所以不能直接写为一个object数组
        String courierStationName = map.get("courierStationName");
        // 判断如果map集合中有courierStationName则该sql需要模糊查询
        if (courierStationName != null && !"".equals(courierStationName)) {
            sql += " and courierStationName like ? ";
            params.add("%" + courierStationName + "%");
        }
        //判断如果map集合中是否有小区名称,则该sql需要进行模糊查询
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {
            sql += " and courierStationCommunityName like ? ";
            params.add("%" + communityName + "%");
        }
        // 判断是否需要进行日期范围查询
        String startTime = map.get("startTime");
//        String endTime = map.get("endTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //判断是否按照id排序
        sql += " order by courierStationId desc";
        // 判断是否分页
        String pn = map.get("pageNum");
        String ps = map.get("pageSize");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?,?";
            int pageNum = Integer.parseInt(pn);//String 转为int类型
            int pageSize = Integer.parseInt(ps);
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
        //组成对象
        Object[] obj = params.toArray(); //形成参数数据来注入数据
        ResultSet resultSet = JDBCDruidUtil.query(sql, obj);

        while (true) {
            try {
                if (!resultSet.next()) break;
                CourierStation courierStation = new CourierStation(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getInt(5),
                        resultSet.getDate(6),
                        resultSet.getInt(7),
                        resultSet.getInt(8),
                        resultSet.getString(9),
                        resultSet.getString(10),
                        resultSet.getString(11),
                        resultSet.getString(12),
                        resultSet.getInt(13)

                );
                list.add(courierStation);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        //形成动态的sql语句
        String sql = "select count(1) from courierstation where 1=1 ";
        List<Object> params = new ArrayList<>();
        String courierStationName = map.get("courierStationName");
        // 判断如果map集合中有courierStationName则该sql需要模糊查询
        if (courierStationName != null && !"".equals(courierStationName)) {
            sql += " and courierStationName like ? ";
            params.add("%" + courierStationName + "%");
        }
        //判断如果map集合中是否有小区名称,则该sql需要进行模糊查询
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {
            sql += " and courierStationCommunityName like ?";
            params.add("%" + communityName + "%");
        }
        // 判断是否需要进行日期范围查询
        String startTime = map.get("startTime");
//        String endTime = map.get("endTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //组成对象
        Object[] obj = params.toArray(); //形成参数数据来注入数据
        ResultSet resultSet = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!resultSet.next()) break;
                count = resultSet.getInt(1);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        JDBCDruidUtil.close();
        return count;
    }



//    @Override
//    public List<Community> getCommunityList(Map<String, String> map) {
//        //用于存放小区
//        List<Community> list = new ArrayList<>();
//        String sql = "select * from community ";
//        String pn = map.get("pageNum");
//        String size = map.get("pageSize");
//        int pageNum = Integer.parseInt(pn);
//        int pageSize = Integer.parseInt(size);
//        sql += " limit ?,? ";
//        Object obj[] = {(pageNum-1)*pageSize,pageSize};
//        ResultSet res = JDBCDruidUtil.query(sql, obj);
////        System.out.println(res);
//        while (true) {
//            try {
//                if (!res.next()) break;
//                Community community = new Community(
//                        res.getInt(1),
//                        res.getString(2),
//                        res.getString(3),
//                        res.getString(4),
//                        res.getString(5),
//                        res.getString(6),
//                        res.getString(7),
//                        res.getString(8),
//                        res.getDate(9),
//                        res.getInt(10),
//                        res.getInt(11),
//                        res.getString(12),
//                        res.getInt(13),
//                        res.getInt(14),
//                        res.getString(15)
//                );
//                list.add(community);
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        JDBCDruidUtil.close();
//        return list;
//    }

//    @Override
//    public int communityCount(Map<String, String> map) {
//        //用于存放小区
//        String sql = "select count(1) from community ";
//        String pn = map.get("pageNum");
//        String size = map.get("pageSize");
//        int pageNum = Integer.parseInt(pn);
//        int pageSize = Integer.parseInt(size);
//        sql += " limit ?,? ";
//        Object obj[] = {(pageNum - 1) * pageSize, pageSize};
//        ResultSet res = JDBCDruidUtil.query(sql, obj);
//        int total=0;
//        while (true) {
//            try {
//                if (!res.next()) break;
//               total=res.getInt(1);
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        JDBCDruidUtil.close();
//        return total;
//    }

    @Override
    public int ModifyCourierStationStatus(Map<String, String> map) {
        String sql=" update courierstation set courierStationStatus=? where courierStationId=? ";
        //
        String Id = map.get("courierStationId");
        int courierStationId=Integer.parseInt(Id);
        // 参数
        List<Object> params=new ArrayList<>();
        //
        String Status = map.get("courierStationStatus");
        int courierStationStatus=Integer.parseInt(Status);
        params.add(courierStationStatus);

        params.add(courierStationId);
        Object obj[]=params.toArray();
        int num=JDBCDruidUtil.update(sql,obj);
        return num;
    }

}
